package com.example.demo.controller;

import com.example.demo.mapper.UserMapper;
import com.example.demo.pojo.User;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Controller
public class UserController {

    @Autowired
    private UserMapper userMapper;

//    查询用户全部信息
    @GetMapping("/queryUserList")
    public List<User> queryUsrList(){
        List<User> userList = userMapper.queryUserList();
        for (User user : userList){
            System.out.println(user);
        }
        return userList;
    }
//添加一个用户
//    注册用户
    @GetMapping("/addUser")
    public String addUser(){
        return "register";
    }
    //修改一个用户
    @GetMapping("/updateUser")
    public String updateUser(){
//        userMapper.updateUser(new User(1,"李四","123456",1000.0));
        return "userFrom";
    }

    @RequestMapping("/user/login")
    public String login(@RequestParam("userName")String name, @RequestParam("passWord") String password,
                        Model model, HttpSession session){
        List<User> userList = userMapper.queryUserList();
        for (User user : userList){
            if (user.getUsername().equals(name) && user.getPassword().equals(password)){
                session.setAttribute("loginUser", user.getUsername());
                Object loginUser = session.getAttribute("loginUser");
                model.addAttribute("loginUser",loginUser);
                return "main";
            }
        }
        model.addAttribute("username", name);
        model.addAttribute("msg", "用户名或密码错误");
        return "index";
    }


    @RequestMapping("/user/register")
    public String register(@RequestParam("registerUsername")String username,@RequestParam("registerPassword") String password,
                           Model model){
        List<User> userList = userMapper.queryUserList();
        for (User user : userList){
            if (user.getUsername().equals(username)){
                model.addAttribute("msg", "用户名已存在");
                return "register";
            }
        }
        model.addAttribute("msg", "注册成功,请返回登录页面登录");
        userMapper.addUser(new User(null,username,password,0.0));
        return "register";
    }

    @RequestMapping("/user/transfer")
    public String transfer(@RequestParam("fromUser")String fromUser,@RequestParam("toUser") String toUser,
                          @RequestParam("transferMoney") Double money, Model model,HttpSession session){
        Double fromUserBlance;
        Double toUserBlance;
        List<User> userList = userMapper.queryUserList();
        String username=null;
        String tousername = null;
        Object loginUser = session.getAttribute("loginUser");
        model.addAttribute("loginUser",loginUser);
        for (User user : userList){
            if (user.getUsername().equals(fromUser)){
                username = user.getUsername();
                if (user.getBlance() < money){
                    model.addAttribute("msg", "余额不足");
                    return "main";
                }
                fromUserBlance=  user.getBlance() - money;
                for (User touser : userList){
                     if (touser.getUsername().equals(toUser)){
                         tousername=touser.getUsername();
                         toUserBlance = touser.getBlance() +money;
                         if (username==tousername){
                             model.addAttribute("msg", "不能给自己转账");
                             return "main";
                         }
                        user.setBlance(fromUserBlance);
                        userMapper.updateUser(user);
                        touser.setBlance(toUserBlance);
                        userMapper.updateUser(touser);
                    }
                }

            }
        }
        if (tousername == null){
            model.addAttribute("msg", "转账失败");
        }else {
            model.addAttribute("msg", "转账成功");
        }
        return "main";
    }

    @RequestMapping("/user/array")
    public String eachArray(Model model){
        Collection<User> users=userMapper.queryUserList();
        model.addAttribute("users",users);
        return "userFrom";
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Integer id, HttpServletResponse servletResponse)throws IOException{
        userMapper.deleteUser(id);
        System.out.println("delete方法执行");
        return "redirect:/user/array";
}
    @GetMapping("/updatePage/{id}")
    public String updatePage(Model model,@PathVariable int id){
        User users = userMapper.queryUserById(id);
        model.addAttribute("users",users);
        return "modifie";
    }


    @PostMapping("/update")
    public String updateUser(Model model, User user, HttpServletRequest request) {
        String id = request.getParameter("id");
        User userById = userMapper.queryUserById(Integer.parseInt(id));
      userMapper.updateUser(user);
        System.out.println(user);
        return "redirect:/user/array";
    }

    //新增数据
    @GetMapping("/addH")
    public String addH(){
        return "add";
    }
    @PostMapping("/add")
    public String save(@RequestParam("username")String username,
                        @RequestParam("password")String password,
                       @RequestParam("blance")Double blance,
                       Model model) {
        User user=new User(null,username,password,blance);
        List<User> users = userMapper.queryUserList();
        for (User usera:users){
            if (usera.getUsername().equals(user.getUsername())){
                model.addAttribute("msg","用户名已存在");
                return "add";
            }
        }
        //表示重置index.html界面并且跳转到index.html界面
        return "redirect:/user/array";
    }


}
